//
//  WLNetWorkRequestManager.m
//  EPA
//
//  Created by lanou3g on 16/4/15.
//  Copyright © 2016年 王玲. All rights reserved.
//

#import "WLNetWorkRequestManager.h"

@interface WLNetWorkRequestManager ()

///存储回调block函数(block这里用copy)
//@property(nonatomic,copy)ResultBlock block;

@end

@implementation WLNetWorkRequestManager

////单利方法(确保只有一个访问对象)
//+ (instancetype)defaultManger{
//    static WLNetWorkRequestManager *manger = nil;
//    @synchronized(self) {
//        if (!manger) {
//            manger = [[WLNetWorkRequestManager alloc] init];
//        }
//    }
//    return manger;
//}

//请求方法
+ (void)requestURL:(NSURL *)url Method:(URLRequestMethod)method Params:(NSDictionary *)param Result:(ResultBlock)resultBlock{
    //isKindOfClass来确定一个对象是否是一个类的成员，或者是派生自该类的成员(派生类:子类)
    //空的可变字符串
    NSMutableString *urlString = [NSMutableString new];
    if ([url isKindOfClass:[NSURL class]]) {
        //拼接url
        [urlString appendString:url.absoluteString];
    }else if ([url isKindOfClass:[NSString class]]){
        [urlString appendString:(NSString *)url];
    }
    if (urlString.length == 0) {
        //如果拼接之后还是空的url就报错
        NSError *error = [NSError errorWithDomain:@"EPA" code:666 userInfo:@{@"reason":@"出错啦😂😂😂"}];
        resultBlock(nil,nil,error);
        return;
    }
    
    //把字典转化为字符串
    WLNetWorkRequestManager *manger = [WLNetWorkRequestManager new];
    NSString *paraStr = [manger strFromDict:param];
    switch (method) {
        case WL_GET:
        {
            if (paraStr.length > 0) {
                [urlString appendFormat:@"?%@",paraStr];
                
            }
            [manger requestURL:[NSURL URLWithString:urlString]resultBlock:resultBlock];
        }
            break;
        case WL_POST:
            
                [manger postURL:urlString ParaStr:paraStr resultBlock:resultBlock];
            
            break;
        default:
            break;
    }
}

//Get网络请求
- (void)requestURL:(NSURL *)url resultBlock:(ResultBlock )block{

    NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:block];
    //开始网络请求
    [dataTask resume];
}

//Post网络请求
- (void)postURL:(NSString *)urlStr ParaStr:(NSString *)paraStr resultBlock:(ResultBlock )block{
    //创建URL
    NSURL *url = [NSURL URLWithString:urlStr];
    //创建可变请求
    NSMutableURLRequest *mRequest = [NSMutableURLRequest requestWithURL:url];
    //设置POST
    //请求体
    [mRequest setHTTPMethod:@"POST"];
    [mRequest setHTTPBody:[paraStr dataUsingEncoding:NSUTF8StringEncoding]];
    //传过来的block参数
    NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:mRequest completionHandler:block];
    //开始网络请求
    [dataTask resume];
}

//把字典传过去返回字符串(url里不能有汉字)
- (NSString *)strFromDict:(NSDictionary *)dict{
    //空字符串
//    NSMutableString *paraStr = [NSMutableString stringWithFormat:@"?"];
    //空数组(将字符串拼回去)
    NSMutableArray *array = [NSMutableArray new];
    for (NSString *key in dict) {
       NSString *str = [NSString stringWithFormat:@"%@=%@",key,dict[key]];
        [array addObject:str];
    }
    //数组拼接字符串
    NSString *param = [array componentsJoinedByString:@"&"];
    NSLog(@"%@",param);
    //返回
    return  param;
}


@end
